package com.holly.unit.deform.api.util;

import com.holly.unit.deform.api.pojo.component.DeformDeptTreeDTO;
import com.holly.unit.deform.api.pojo.component.OrgIdDTO;
import com.holly.unit.system.api.pojo.organization.HrOrganizationDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述: 组织机构处理并查找树级数据
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/16 11:46
 */
public class OrgChildrenUtil {
    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是s将HrOrganizationDTO类型的list集合转换成DeformDeptTreeDTO类型的集合
     */
    public static List<DeformDeptTreeDTO> wrapTreeDataToTreeList(List<HrOrganizationDTO> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<OrgIdDTO> idList = new ArrayList<OrgIdDTO>();
        List<DeformDeptTreeDTO> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            HrOrganizationDTO depart = recordList.get(i);
            records.add(new DeformDeptTreeDTO(depart));
        }
        List<DeformDeptTreeDTO> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * 获取 OrgIdDTO
     * @param recordList
     * @return
     */
    public static List<OrgIdDTO> wrapTreeDataToDepartIdTreeList(List<HrOrganizationDTO> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<OrgIdDTO> idList = new ArrayList<OrgIdDTO>();
        List<DeformDeptTreeDTO> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            HrOrganizationDTO depart = recordList.get(i);
            records.add(new DeformDeptTreeDTO(depart));
        }
        findChildren(records, idList);
        return idList;
    }

    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<DeformDeptTreeDTO> findChildren(List<DeformDeptTreeDTO> recordList,
                                                         List<OrgIdDTO> departIdList) {

        List<DeformDeptTreeDTO> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            DeformDeptTreeDTO branch = recordList.get(i);
            if (branch.getOrgParentId() == -1) {
                treeList.add(branch);
                OrgIdDTO OrgIdDTO = new OrgIdDTO().convert(branch);
                departIdList.add(OrgIdDTO);
            }
        }
        getGrandChildren(treeList,recordList,departIdList);

        //idList = departIdList;
        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<DeformDeptTreeDTO> treeList,List<DeformDeptTreeDTO> recordList,List<OrgIdDTO> idList) {

        for (int i = 0; i < treeList.size(); i++) {
            DeformDeptTreeDTO model = treeList.get(i);
            OrgIdDTO idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                DeformDeptTreeDTO m = recordList.get(i1);
                if (m.getOrgParentId()!=null && m.getOrgParentId().equals(model.getOrgId())) {
                    model.getChildren().add(m);
                    OrgIdDTO dim = new OrgIdDTO().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }

    }


    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<DeformDeptTreeDTO> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            DeformDeptTreeDTO model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setIsLeaf(true);
            }else{
                setEmptyChildrenAsNull(model.getChildren());
                model.setIsLeaf(false);
            }
        }
    }
    
}
